<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>DSF2FLAC: dop_packer.cpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">DSF2FLAC
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">dop_packer.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * dsf2flac - http://code.google.com/p/dsf2flac/</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * A file conversion tool for translating dsf dsd audio files into</span>
<a name="l00005"></a>00005 <span class="comment"> * flac pcm audio files.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Copyright (c) 2013 by respective authors.</span>
<a name="l00008"></a>00008 <span class="comment"> *</span>
<a name="l00009"></a>00009 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
<a name="l00010"></a>00010 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<a name="l00011"></a>00011 <span class="comment"> * the Free Software Foundation; either version 2 of the License, or</span>
<a name="l00012"></a>00012 <span class="comment"> * (at your option) any later version.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
<a name="l00015"></a>00015 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00016"></a>00016 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<a name="l00017"></a>00017 <span class="comment"> * GNU General Public License for more details.</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
<a name="l00020"></a>00020 <span class="comment"> * along with this program; if not, write to the Free Software</span>
<a name="l00021"></a>00021 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span>
<a name="l00022"></a>00022 <span class="comment"> *</span>
<a name="l00023"></a>00023 <span class="comment"> *</span>
<a name="l00024"></a>00024 <span class="comment"> * Acknowledgements</span>
<a name="l00025"></a>00025 <span class="comment"> *</span>
<a name="l00026"></a>00026 <span class="comment"> * Many thanks to the following authors and projects whose work has greatly</span>
<a name="l00027"></a>00027 <span class="comment"> * helped the development of this tool.</span>
<a name="l00028"></a>00028 <span class="comment"> *</span>
<a name="l00029"></a>00029 <span class="comment"> *</span>
<a name="l00030"></a>00030 <span class="comment"> * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)</span>
<a name="l00031"></a>00031 <span class="comment"> * SACD Ripper (http://code.google.com/p/sacd-ripper/)</span>
<a name="l00032"></a>00032 <span class="comment"> * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)</span>
<a name="l00033"></a>00033 <span class="comment"> * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)</span>
<a name="l00034"></a>00034 <span class="comment"> * Jesus R - www.sonore.us</span>
<a name="l00035"></a>00035 <span class="comment"> *</span>
<a name="l00036"></a>00036 <span class="comment"> */</span>
<a name="l00037"></a>00037 
<a name="l00038"></a>00038 <span class="preprocessor">#include &quot;dop_packer.h&quot;</span>
<a name="l00039"></a>00039 
<a name="l00040"></a>00040 dsf2flac_int32 odd_marker = 0x00050000;
<a name="l00041"></a>00041 dsf2flac_int32 even_marker  = 0xFFFA0000;
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="classDopPacker.html#a4df822e610b9b0d36f2a0a2334cc80b7">00043</a> <a class="code" href="classDopPacker.html#a4df822e610b9b0d36f2a0a2334cc80b7">DopPacker::DopPacker</a>(DsdSampleReader *r) {
<a name="l00044"></a>00044         reader = r;
<a name="l00045"></a>00045         <span class="comment">// buffer length needs to be at least 2 chars long</span>
<a name="l00046"></a>00046         <span class="keywordflow">if</span> (2 &gt; reader-&gt;getBufferLength())
<a name="l00047"></a>00047                 reader-&gt;setBufferLength(2);
<a name="l00048"></a>00048 }
<a name="l00049"></a>00049 
<a name="l00050"></a><a class="code" href="classDopPacker.html#ab7eb362b4eee8564c464224e38af0d7f">00050</a> <a class="code" href="classDopPacker.html#ab7eb362b4eee8564c464224e38af0d7f">DopPacker::~DopPacker</a>() {
<a name="l00051"></a>00051 
<a name="l00052"></a>00052 }
<a name="l00053"></a>00053 
<a name="l00054"></a>00054 <span class="comment">//unsigned char reverse(unsigned char b) {</span>
<a name="l00055"></a>00055 <span class="comment">//   b = (b &amp; 0xF0) &gt;&gt; 4 | (b &amp; 0x0F) &lt;&lt; 4;</span>
<a name="l00056"></a>00056 <span class="comment">//   b = (b &amp; 0xCC) &gt;&gt; 2 | (b &amp; 0x33) &lt;&lt; 2;</span>
<a name="l00057"></a>00057 <span class="comment">//   b = (b &amp; 0xAA) &gt;&gt; 1 | (b &amp; 0x55) &lt;&lt; 1;</span>
<a name="l00058"></a>00058 <span class="comment">//   return b;</span>
<a name="l00059"></a>00059 <span class="comment">//}</span>
<a name="l00060"></a>00060 
<a name="l00061"></a>00061 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> reverse(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> x)
<a name="l00062"></a>00062 {
<a name="l00063"></a>00063     <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> table[] = {
<a name="l00064"></a>00064         0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
<a name="l00065"></a>00065         0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
<a name="l00066"></a>00066         0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
<a name="l00067"></a>00067         0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
<a name="l00068"></a>00068         0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
<a name="l00069"></a>00069         0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
<a name="l00070"></a>00070         0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
<a name="l00071"></a>00071         0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
<a name="l00072"></a>00072         0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
<a name="l00073"></a>00073         0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
<a name="l00074"></a>00074         0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
<a name="l00075"></a>00075         0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
<a name="l00076"></a>00076         0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
<a name="l00077"></a>00077         0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
<a name="l00078"></a>00078         0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
<a name="l00079"></a>00079         0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
<a name="l00080"></a>00080         0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
<a name="l00081"></a>00081         0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
<a name="l00082"></a>00082         0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
<a name="l00083"></a>00083         0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
<a name="l00084"></a>00084         0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
<a name="l00085"></a>00085         0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
<a name="l00086"></a>00086         0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
<a name="l00087"></a>00087         0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
<a name="l00088"></a>00088         0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
<a name="l00089"></a>00089         0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
<a name="l00090"></a>00090         0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
<a name="l00091"></a>00091         0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
<a name="l00092"></a>00092         0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
<a name="l00093"></a>00093         0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
<a name="l00094"></a>00094         0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
<a name="l00095"></a>00095         0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
<a name="l00096"></a>00096     };
<a name="l00097"></a>00097     <span class="keywordflow">return</span> table[x];
<a name="l00098"></a>00098 }
<a name="l00099"></a>00099 
<a name="l00100"></a><a class="code" href="classDopPacker.html#a5afb6411fdfeefc7f3303883c6501cca">00100</a> <span class="keywordtype">void</span> <a class="code" href="classDopPacker.html#a5afb6411fdfeefc7f3303883c6501cca">DopPacker::pack_buffer</a>(dsf2flac_int32 *buffer, dsf2flac_uint32 bufferLen) {
<a name="l00101"></a>00101         <span class="comment">// check the buffer seems sensible</span>
<a name="l00102"></a>00102         div_t d = div(bufferLen,reader-&gt;getNumChannels());
<a name="l00103"></a>00103         <span class="keywordflow">if</span> (d.rem) {
<a name="l00104"></a>00104                 fputs(<span class="stringliteral">&quot;Buffer length is not a multiple of getNumChannels()&quot;</span>,stderr);
<a name="l00105"></a>00105                 exit(EXIT_FAILURE);
<a name="l00106"></a>00106         }
<a name="l00107"></a>00107         <span class="comment">// get the sample buffer</span>
<a name="l00108"></a>00108         boost::circular_buffer&lt;dsf2flac_uint8&gt;* buff = reader-&gt;getBuffer();
<a name="l00109"></a>00109         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;d.quot ; i++) {
<a name="l00110"></a>00110 
<a name="l00111"></a>00111                 dsf2flac_int32 packed_sample;
<a name="l00112"></a>00112                 dsf2flac_uint8 byte1;
<a name="l00113"></a>00113                 dsf2flac_uint8 byte2;
<a name="l00114"></a>00114 
<a name="l00115"></a>00115                 <span class="comment">// step the buffer right away so that the first sample sits in position 1 in the buffer (second in position 0)</span>
<a name="l00116"></a>00116                 reader-&gt;step();
<a name="l00117"></a>00117 
<a name="l00118"></a>00118                 <span class="keywordflow">for</span> (dsf2flac_uint32 c=0; c&lt;reader-&gt;getNumChannels(); c++) {
<a name="l00119"></a>00119 
<a name="l00120"></a>00120                         <span class="keywordflow">if</span> ((reader-&gt;getPosition(8) % 32) != 0)
<a name="l00121"></a>00121                                 packed_sample = even_marker;
<a name="l00122"></a>00122                         <span class="keywordflow">else</span>
<a name="l00123"></a>00123                                 packed_sample = odd_marker;
<a name="l00124"></a>00124 
<a name="l00125"></a>00125                         byte1 = buff[c][1];
<a name="l00126"></a>00126                         byte2 = buff[c][0];
<a name="l00127"></a>00127 
<a name="l00128"></a>00128                         <span class="keywordflow">if</span> (reader-&gt;msbIsPlayedFirst()) {
<a name="l00129"></a>00129                                 byte1 = reverse(byte1);
<a name="l00130"></a>00130                                 byte2 = reverse(byte2);
<a name="l00131"></a>00131                         }
<a name="l00132"></a>00132 
<a name="l00133"></a>00133                         packed_sample += ((dsf2flac_int32) byte1) &lt;&lt; 8;
<a name="l00134"></a>00134                         packed_sample += ((dsf2flac_int32) byte2);
<a name="l00135"></a>00135 
<a name="l00136"></a>00136                         buffer[i*reader-&gt;getNumChannels()+c] = packed_sample;
<a name="l00137"></a>00137                 }
<a name="l00138"></a>00138 
<a name="l00139"></a>00139                 reader-&gt;step();
<a name="l00140"></a>00140         }
<a name="l00141"></a>00141 }
<a name="l00142"></a>00142 
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Fri Sep 13 2013 14:42:01 for DSF2FLAC by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
